layui.use(['form', 'layer','laydate','upload'], function(){
	  var form = layui.form
	  ,layer = layui.layer
	  ,laydate = layui.laydate
	  ,upload = layui.upload
	  ,$ = layui.$;
	  
	  var action = getParameter('action');
	  if(action == "add"){
		  $("#usersId").val(user.usersId);
	  }else {
		  var proId = getParameter('proId');
		  var url = "../product/findById.action";
		  var data = {"proId":proId};
		  $.ajax({
			  url:url,
			  data:data,
			  success:function(msg){
				  form.val('myform',{
					  'proId':msg.proId,
					  'userId':msg.userId,
					  'proName':msg.proName,
					  'proTypeId':msg.proTypeId,
					  'proPrice':msg.proPrice,
					  'proColor':msg.proColor,
					  'proCapacity':msg.proCapacity,
					  'proRemark':msg.proRemark
				  });
			  }
		  });
	  }
	  
	//查询产品类型
	  var url = "../productType/findAllNameOrId.action";
	  var data = null;
	  $.ajax({
		  url:url,
		  data:data,
		  success:function(msg){
			  for(var i=0;i<msg.length;i++){
				  $("#proTypeId").append('<option value="'+msg[i].proTypeId+'">'+msg[i].proTypeName+'</option>');
			  }
			  form.render();//菜单渲染 把内容加载进去
		  }
	  });
	  
	 $(".upload").click(function(){
		 var obj = event.target;
		 var str = $(obj).text();
		 var strs = str.substring(1,str.length);
		 layer.open({
			  title: strs
			  ,type: 2
			  ,area: ['60%','74%']
			  ,content: 'uploadFile.html'
		  })
	 });
	 
	 $("#add-product").click(function(){
		 var arr = $("#myform").serialize();
		 var value = $(".layui-input");
		 for(var i=0;i<value.length;i++){
			 var va = $(value[i]).val();
			 if(va == null || va == ""){
				 layer.msg("请把信息填写完善!",{icon:5,time:1000,anim:6});
				 return ;
			 }
		 }
		 var url = "../product/saveOrUpdate.action";
		 var data = arr;
		 $.ajax({
			 url:url,
			 data:data,
			 success:function(msg){
				 if(msg.state == "1"){
					 layer.msg("保存成功!",{icon:1,time:1000},function(){
						 var index = parent.layer.getFrameIndex(window.name);  
	    				    parent.layer.close(index);
					 })
					 
				 }else {
					 layer.msg("保存失败!",{icon:1,time:1000},function(){
						 var index = parent.layer.getFrameIndex(window.name);  
	    				    parent.layer.close(index);
					 })
				 }
			 }
		 });
	 });
});